
$(function() {


    $("#birthday").datebox('calendar').calendar({
        validator:function(beginDate){
            var date = new Date();
                date.setDate(date.getDate()-1); 
            return beginDate < date;
        }
    });
    toastr.options.positionClass = 'toast-center-center';

    //alert($("#rmbUser").switchbutton("options").checked);
    //回车事件
/*	$('#password').passwordbox('textbox').keydown(function (e) {
        if(e.keyCode == 13) {
           login();
        }
    });	*/
});

function register(){
    var loginName = $("#loginName").textbox("getValue"); //$("#login_form").find("input[name='loginName']").val();
    var password = $("#password").passwordbox("getValue"); 
    var rePassword = $("#rePassword").passwordbox("getValue"); 
    var birthday = $("#birthday").datebox('getValue');
    var sex = $("#sex").switchbutton("options").checked ? 2:1;

    var reg = new RegExp(/^[0-9A-Za-z]{3,20}$/);
    if (!reg.test(loginName)) {
        $("#errMsgBar").show();
        $("#errMsgBar").html("登录名长度由4至20个字母或数字组成");        
        return;
    }

    reg = new RegExp(/^[0-9A-Za-z]{6,20}$/);
    if (!reg.test(password)) {
        $("#errMsgBar").show();
        $("#errMsgBar").html("密码长度由6至20个字母或数字组成");        
        return;
    }    
/*    if(loginName.length == 0){
        $("#errMsgBar").show();
        $("#errMsgBar").html("请输入登录名");
        return;
    }
    if(loginName.length > 20){
        $("#errMsgBar").show();
        $("#errMsgBar").html("登录名长度超过限制");
        return;
    }  */

/*    if(password.length == 0){
        $("#errMsgBar").show();
        $("#errMsgBar").html("请输入密码");
        return;
    }
    if(password.length > 20){
        $("#errMsgBar").show();
        $("#errMsgBar").html("密码长度超过限制");
        return;
    } */ 
    if(rePassword.length == 0){
        $("#errMsgBar").show();
        $("#errMsgBar").html("请再次输入密码");
        return;
    }    
    if(rePassword != password){
        $("#errMsgBar").show();
        $("#errMsgBar").html("两次输入密码不一致");
        return;
    }
    if(birthday.length == 0){
        $("#errMsgBar").show();
        $("#errMsgBar").html("请选择出生日期");
        return;
    }      
    var param = {
        "loginName":loginName,
        "password":password,
        "birthday":birthday,
        "sex":sex
        };
    //alert(JSON.stringify(param));
    $("#registerBtn").linkbutton('disable');
    $.ajax({
       // headers: {
        //    app: 'PLATFORM',
        //    platform: 'PC',
        //    imei: ''
       // },
        type:"post",
        url:"/note/register_do",
        //contentType:"application/json;charset=utf-8",
        data:{param:AESEncrypt(param)},
        dataType:'json',
        success:function(ret){
            if(ret.code == 0){ //成功
               toastr.success("注册成功，正在跳转至登录页面");
               setTimeout(function(){
                    window.location.href = "/note/login";
                }, 2000);
               
            }else{
              //alert(ret.msg);
                $("#errMsgBar").show();
                $("#errMsgBar").html(ret.msg);
            }
        },
        error:function(ret) {
            toastr.error("注册失败");
        },
        complete:function () {
            $("#registerBtn").linkbutton('enable');
        }
    });    
}

